//
//  CLHomeWeatherListCity.m
//  CareLink
//
//  Created by 刘家强 on 16/8/19.
//  Copyright © 2016年 Seven Team. All rights reserved.
//

#import "CLHomeWeatherListCity.h"
#import "CLHomeWeatherModel.h"

// cellId
static NSString *liftTabelViewCellId = @"liftTabelViewCellId";
static NSString *rightTabelViewCellId = @"rightTabelViewCellId";

@interface CLHomeWeatherListCity () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, weak) UITableView *liftTabelView;     // 左
@property (nonatomic, weak) UITableView *rightTabelView;    // 右
@property (nonatomic, strong) NSArray<CLHomeWeatherModel *> *cityList;
@property (nonatomic, assign) NSInteger rightPath;

@end

@implementation CLHomeWeatherListCity

- (instancetype)init {
    self = [super init];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self loadCityList];
    [self setupUI];
}

// 加载数据
- (void)loadCityList {
    // 获取路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"cityList.plist" ofType:nil];

    // 获取临时数组
    NSArray *tempArr = [NSArray arrayWithContentsOfFile:path];

    // 转模型
    self.cityList = [NSArray yy_modelArrayWithClass:[CLHomeWeatherModel class] json:tempArr];
}

- (void)setupUI {
    // 创建tabelView
    UITableView *liftTabelView = [[UITableView alloc] init];
    UITableView *rightTabelView = [[UITableView alloc] init];

    // 添加到父控件上
    [self.view addSubview:liftTabelView];
    [self.view addSubview:rightTabelView];

    // 全局化
    self.liftTabelView = liftTabelView;
    self.rightTabelView = rightTabelView;

    // 设置代理
    liftTabelView.delegate = self;
    liftTabelView.dataSource = self;
    rightTabelView.delegate = self;
    rightTabelView.dataSource = self;

    // 注册单元格
    [liftTabelView registerClass:[UITableViewCell class] forCellReuseIdentifier:liftTabelViewCellId];
    [rightTabelView registerClass:[UITableViewCell class] forCellReuseIdentifier:rightTabelViewCellId];

    // 自动布局
    [liftTabelView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.equalTo(self.view);
//        make.top.left.offset(10);
//        make.bottom.offset(-10)
        make.width.offset(140);
    }];
    [rightTabelView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.equalTo(self.view);
        make.width.offset(140);
    }];
}

// 有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.liftTabelView) {
        return self.cityList.count;
    } else {
        return self.cityList[self.rightPath].cities.count;
    }
}

// view已经显示.
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

// cell的点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.liftTabelView) {
        self.rightPath = [tableView indexPathForSelectedRow].row;
        [self.rightTabelView reloadData];

    } else {
        NSString *cityStr = self.cityList[self.rightPath].cities[[tableView indexPathForSelectedRow].row];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"kTypeButtonClick" object:cityStr userInfo:nil];
    }
}

// cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.liftTabelView) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:liftTabelViewCellId forIndexPath:indexPath];
        cell.textLabel.text = self.cityList[indexPath.row].name;
        return cell;
    } else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:rightTabelViewCellId forIndexPath:indexPath];
        cell.textLabel.text = self.cityList[self.rightPath].cities[indexPath.row];
//        cell.textLabel.font = [UIFont systemFontOfSize:12];
        return cell;
    }
}

@end
